// subcontract/Home/orderDetails/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    bubbleShow: false,
    orderID: '',//订单id
    orderinfo: {},//订单详情
    payShow: false,//支付状态
    current: Date.now(),//当前时间戳
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
  },
  /**商品列表 */
  async getGoodsList() {
    let object = {
      page_no: this.data.page,
      is_rand: 1
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  onClose() {
    this.setData({
      payShow: !this.data.payShow
    })
  },
  /**支付方式 */
  async unifiedpay(from, order_id) {
    let { orderinfo } = this.data
    let object = {
      order_id: orderinfo.id,
      from: 'order',
      pay_way: 1
    }
    const { code, data } = await getApp().api.unifiedpay(object)
    if (code == 1) {
      const ret = await getApp().requestPay(data)
      if (ret.errMsg == 'requestPayment:ok') {
        getApp().showToast('支付成功!')
        this.getOrderDetail(this.data.orderID)
      } else {
        getApp().showToast('支付失败！')
      }
    }
  },
  /**立即支付 */
  payment() {
    this.setData({
      payShow: !this.data.payShow,
    })
  },
  /**删除订单 */
  async orderDel(event) {
    let { id } = event.currentTarget.dataset
    console.log(event);
    let object = {
      id
    }
    const ret = await wx.showModal({
      title: '提示',
      content: '您确定要删除此订单嘛?',
    })
    if (ret.confirm) {
      const { code, daat } = await getApp().api.orderDel(object)
      if (code == 1) {
        wx.navigateBack()
      }
    }

  },
  /**关闭订单 */
  async ordercancel(event) {
    let { id } = event.currentTarget.dataset
    console.log(event);
    let object = {
      id
    }
    const ret = await wx.showModal({
      title: '提示',
      content: '您确定要关闭此订单嘛?',
    })
    if (ret.confirm) {
      const { code, daat } = await getApp().api.ordercancel(object)
      if (code == 1) {
        wx.navigateBack()
      }
    }
  },
  /**订单详情 */
  async getOrderDetail(id) {
    let object = {
      id
    }
    const { code, data } = await getApp().api.getOrderDetail(object)
    if (code == 1) {
      wx.setNavigationBarTitle({
        title: data.order_status_desc,
      })
      this.setData({
        orderinfo: data
      })
    }
  },
  showBubble() {
    this.setData({
      bubbleShow: !this.data.bubbleShow,
    });
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderID: options.id
    })
    this.getOrderDetail(options.id)
    this.getGoodsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrderDetail(this.data.orderID)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getGoodsList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})